package com.apache.tools;

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.dom4j.Document;

/**
 * xml转换工具类
 *
 */
public class XmlTools {

	/**
	 * doc文档转换成html，与xsl模板合成html
	 * @param doc
	 * @param xslfile
	 * @return
	 */
	public static String transform(Document doc, String xslfile) {
		String outS = "";
		try {

			org.w3c.dom.Document w3cdoc = parse(doc);

			StringWriter stringWriter = new StringWriter();
			javax.xml.transform.TransformerFactory tFactory = javax.xml.transform.TransformerFactory.newInstance();

			DOMSource domSource = new DOMSource(w3cdoc);

			StreamSource sxsl = new StreamSource(xslfile);

			Transformer transformer = tFactory.newTransformer(sxsl);
			transformer.setOutputProperty("encoding", "UTF-8");
			transformer.setOutputProperty(OutputKeys.METHOD, "html");

			StreamResult result = new StreamResult(stringWriter);

			transformer.transform(domSource, result);

			outS = result.getWriter().toString();

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}

		return outS;

	}

	/**
	 * xml文件转换成html，与xsl模板合成html
	 * @param xmlfile
	 * @param xslfile
	 * @return
	 */
	public static String transform(String xmlfile, String xslfile) {
		// String file_temp = resultfile+".temp";
		String outS = "";
		try {

			StringWriter result = new StringWriter();
			javax.xml.transform.TransformerFactory tFactory = javax.xml.transform.TransformerFactory.newInstance();

			StreamSource sxsl = new StreamSource(xslfile);
			StreamSource sxml = new StreamSource(xmlfile);

			Transformer transformer = tFactory.newTransformer(sxsl);
			transformer.setOutputProperty("encoding", "UTF-8");
			transformer.setOutputProperty(OutputKeys.METHOD, "html");

			transformer.setParameter("no", "8");
			transformer.setParameter("width", "21");
			transformer.setParameter("pageid", "1");
			transformer.setParameter("pagecount", "21");
			transformer.setParameter("start", "1");
			transformer.setParameter("end", "20");
			transformer.setParameter("size", "5");

			transformer.transform(sxml, new StreamResult(result));

			outS = result.toString();

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}

		return outS;
	}

	/**
	 * dom4j转换成w3c对象
	 * @param doc
	 * @return
	 * @throws Exception
	 */
	public static org.w3c.dom.Document parse(Document doc) throws Exception {
		if (doc == null) {
			return (null);
		}
		java.io.StringReader reader = new java.io.StringReader(doc.asXML());
		org.xml.sax.InputSource source = new org.xml.sax.InputSource(reader);
		javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory = javax.xml.parsers.DocumentBuilderFactory
				.newInstance();
		javax.xml.parsers.DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
		return (documentBuilder.parse(source));
	}
}
